package com.add.system;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1PolyMorph;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CardBookCmd {
  private static final Log _log = LogFactory.getLog(CardBookCmd.class);
  
  private static CardBookCmd _instance;
  
  public static CardBookCmd get() {
    if (_instance == null)
      _instance = new CardBookCmd(); 
    return _instance;
  }
  
  public boolean Cmd(L1PcInstance pc, String cmd) {
    try {
      ACard card;
      boolean ok = false;
      String str;
      switch ((str = cmd).hashCode()) {
        case -7322848:
          if (!str.equals("cardset2"))
            break; 
          CardAllSet(pc);
          ok = true;
          break;
        case 553953106:
          if (!str.equals("cardset"))
            break; 
          CardSet(pc);
          ok = true;
          break;
        case 561663196:
          if (!str.equals("polycard"))
            break; 
          card = ACardTable.get().getCard(pc.getCardId());
          if (card != null)
            if (pc.getQuest().get_step(card.getQuestId()) != 0) {
              if (card.getPolyId() != 0) {
                if (card.getPolyItemId() != 0) {
                  if (pc.getInventory().checkItem(card.getPolyItemId(), card.getPolyItemCount())) {
                    L1PolyMorph.doPoly((L1Character)pc, card.getPolyId(), card.getPolyTime(), 1);
                    pc.getInventory().consumeItem(card.getPolyItemId(), card.getPolyItemCount());
                  } else {
                    pc.sendPackets((ServerBasePacket)new S_SystemMessage("变身需求道具不足"));
                  } 
                } else {
                  L1PolyMorph.doPoly((L1Character)pc, card.getPolyId(), card.getPolyTime(), 1);
                } 
              } else {
                pc.sendPackets((ServerBasePacket)new S_SystemMessage("无法变身"));
              } 
            } else {
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("尚未登入卡册"));
            }  
          ok = true;
          break;
      } 
      if (ok)
        return true; 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return false;
  }
  
  private void CardAllSet(L1PcInstance pc) {
    try {
      int str = 0;
      int dex = 0;
      int con = 0;
      int Int = 0;
      int wis = 0;
      int cha = 0;
      int ac = 0;
      int hp = 0;
      int mp = 0;
      int hpr = 0;
      int mpr = 0;
      int dmg = 0;
      int bdmg = 0;
      int hit = 0;
      int bhit = 0;
      int dr = 0;
      int mdr = 0;
      int sp = 0;
      int mhit = 0;
      int mr = 0;
      int f = 0;
      int wind = 0;
      int w = 0;
      int e = 0;
      int i = 0;
      while (i <= ACardTable.get().ACardSize()) {
        ACard card = ACardTable.get().getCard(i);
        if (card != null && pc.getQuest().get_step(card.getQuestId()) != 0) {
          str += card.getAddStr();
          dex += card.getAddDex();
          con += card.getAddCon();
          Int += card.getAddInt();
          wis += card.getAddWis();
          cha += card.getAddCha();
          ac += card.getAddAc();
          hp += card.getAddHp();
          mp += card.getAddMp();
          hpr += card.getAddHpr();
          mpr += card.getAddMpr();
          dmg += card.getAddDmg();
          bdmg += card.getAddBowDmg();
          hit += card.getAddHit();
          bhit += card.getAddBowHit();
          dr += card.getAddDmgR();
          mdr += card.getAddMagicDmgR();
          sp += card.getAddSp();
          mhit += card.getAddMagicHit();
          mr += card.getAddMr();
          f += card.getAddFire();
          wind += card.getAddWind();
          e += card.getAddEarth();
          w += card.getAddWater();
        } 
        i++;
      } 
      i = 0;
      while (i <= CardSetTable.get().CardCardSize()) {
        CardPolySet cards = CardSetTable.get().getCard(i);
        if (cards != null && pc.getQuest().get_step(cards.getQuestId()) != 0) {
          str += cards.getAddStr();
          dex += cards.getAddDex();
          con += cards.getAddCon();
          Int += cards.getAddInt();
          wis += cards.getAddWis();
          cha += cards.getAddCha();
          ac += cards.getAddAc();
          hp += cards.getAddHp();
          mp += cards.getAddMp();
          hpr += cards.getAddHpr();
          mpr += cards.getAddMpr();
          dmg += cards.getAddDmg();
          bdmg += cards.getAddBowDmg();
          hit += cards.getAddHit();
          bhit += cards.getAddBowHit();
          dr += cards.getAddDmgR();
          mdr += cards.getAddMagicDmgR();
          sp += cards.getAddSp();
          mhit += cards.getAddMagicHit();
          mr += cards.getAddMr();
          f += cards.getAddFire();
          wind += cards.getAddWind();
          e += cards.getAddEarth();
          w += cards.getAddWater();
        } 
        i++;
      } 
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.append("力量 +" + str + ",");
      stringBuilder.append("敏捷 +" + dex + ",");
      stringBuilder.append("体质 +" + con + ",");
      stringBuilder.append("智力 +" + Int + ",");
      stringBuilder.append("精神 +" + wis + ",");
      stringBuilder.append("魅力 +" + cha + ",");
      stringBuilder.append("防御提升 +" + ac + ",");
      stringBuilder.append("HP +" + hp + ",");
      stringBuilder.append("MP +" + mp + ",");
      stringBuilder.append("血量回复 +" + hpr + ",");
      stringBuilder.append("魔力回复 +" + mpr + ",");
      stringBuilder.append("近距离伤害 +" + dmg + ",");
      stringBuilder.append("远距离伤害 +" + bdmg + ",");
      stringBuilder.append("近距离命中 +" + hit + ",");
      stringBuilder.append("远距离命中 +" + bhit + ",");
      stringBuilder.append("物理伤害减免 +" + dr + ",");
      stringBuilder.append("魔法伤害减免 +" + mdr + ",");
      stringBuilder.append("魔攻 +" + sp + ",");
      stringBuilder.append("魔法命中 +" + mhit + ",");
      stringBuilder.append("魔法防御 +" + mr + ",");
      stringBuilder.append("火属性防御 +" + f + ",");
      stringBuilder.append("风属性防御 +" + wind + ",");
      stringBuilder.append("地属性防御 +" + e + ",");
      stringBuilder.append("水属性防御 +" + w + ",");
      String[] clientStrAry = stringBuilder.toString().split(",");
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "card_11", clientStrAry));
    } catch (Exception e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } 
  }
  
  private void CardSet(L1PcInstance pc) {
    try {
      StringBuilder stringBuilder = new StringBuilder();
      int i = 0;
      while (i <= CardSetTable.get().CardCardSize()) {
        CardPolySet cards = CardSetTable.get().getCard(i);
        if (cards != null) {
          stringBuilder.append(String.valueOf(String.valueOf(cards.getMsg1())) + ",");
          int k = 0;
          int j = 0;
          while (j < (cards.getNeedQuest()).length) {
            if (pc.getQuest().get_step(cards.getNeedQuest()[j]) != 0) {
              stringBuilder.append(String.valueOf(String.valueOf(cards.getNeedName()[j])) + "(登入),");
              k++;
            } else {
              stringBuilder.append(String.valueOf(String.valueOf(cards.getNeedName()[j])) + "(未登),");
            } 
            j++;
          } 
          if (k == (cards.getNeedQuest()).length) {
            if (cards.getAddStr() != 0)
              stringBuilder.append("力量 +" + cards.getAddStr() + ","); 
            if (cards.getAddDex() != 0)
              stringBuilder.append("敏捷 +" + cards.getAddDex() + ","); 
            if (cards.getAddCon() != 0)
              stringBuilder.append("体质 +" + cards.getAddCon() + ","); 
            if (cards.getAddInt() != 0)
              stringBuilder.append("智力 +" + cards.getAddInt() + ","); 
            if (cards.getAddWis() != 0)
              stringBuilder.append("精神 +" + cards.getAddWis() + ","); 
            if (cards.getAddCha() != 0)
              stringBuilder.append("魅力 +" + cards.getAddCha() + ","); 
            if (cards.getAddAc() != 0)
              stringBuilder.append("防御提升 +" + cards.getAddAc() + ","); 
            if (cards.getAddHp() != 0)
              stringBuilder.append("HP +" + cards.getAddHp() + ","); 
            if (cards.getAddMp() != 0)
              stringBuilder.append("MP +" + cards.getAddMp() + ","); 
            if (cards.getAddHpr() != 0)
              stringBuilder.append("血量回复 +" + cards.getAddHpr() + ","); 
            if (cards.getAddMpr() != 0)
              stringBuilder.append("魔力回复 +" + cards.getAddMpr() + ","); 
            if (cards.getAddDmg() != 0)
              stringBuilder.append("近距离伤害 +" + cards.getAddDmg() + ","); 
            if (cards.getAddBowDmg() != 0)
              stringBuilder.append("远距离伤害 +" + cards.getAddBowDmg() + ","); 
            if (cards.getAddHit() != 0)
              stringBuilder.append("近距离命中 +" + cards.getAddHit() + ","); 
            if (cards.getAddBowHit() != 0)
              stringBuilder.append("远距离命中 +" + cards.getAddBowHit() + ","); 
            if (cards.getAddDmgR() != 0)
              stringBuilder.append("物理伤害减免 +" + cards.getAddDmgR() + ","); 
            if (cards.getAddMagicDmgR() != 0)
              stringBuilder.append("魔法伤害减免 +" + cards.getAddMagicDmgR() + ","); 
            if (cards.getAddSp() != 0)
              stringBuilder.append("魔攻 +" + cards.getAddSp() + ","); 
            if (cards.getAddMagicHit() != 0)
              stringBuilder.append("魔法命中 +" + cards.getAddMagicHit() + ","); 
            if (cards.getAddMr() != 0)
              stringBuilder.append("魔法防御 +" + cards.getAddMr() + ","); 
            if (cards.getAddFire() != 0)
              stringBuilder.append("火属性防御 +" + cards.getAddFire() + ","); 
            if (cards.getAddWind() != 0)
              stringBuilder.append("风属性防御 +" + cards.getAddWind() + ","); 
            if (cards.getAddEarth() != 0)
              stringBuilder.append("地属性防御 +" + cards.getAddEarth() + ","); 
            if (cards.getAddWater() != 0)
              stringBuilder.append("水属性防御 +" + cards.getAddWater() + ","); 
            stringBuilder.append("<以上为此套卡能力加成>,");
          } 
        } 
        i++;
      } 
      String[] clientStrAry = stringBuilder.toString().split(",");
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "card_10", clientStrAry));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public boolean PolyCmd(L1PcInstance pc, String cmd) {
    try {
      boolean ok = false;
      StringBuilder stringBuilder = new StringBuilder();
      int i = 0;
      while (i <= ACardTable.get().ACardSize()) {
        ACard card = ACardTable.get().getCard(i);
        if (card != null && cmd.equals(card.getCmd())) {
          pc.setCarId(i);
          stringBuilder.append(String.valueOf(String.valueOf(card.getMsg2())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddStr())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddDex())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddCon())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddInt())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddWis())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddCha())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddAc())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddHp())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddMp())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddHpr())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddMpr())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddDmg())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddBowDmg())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddHit())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddBowHit())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddDmgR())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddMagicDmgR())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddSp())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddMagicHit())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddMr())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddFire())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddWind())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddEarth())) + ",");
          stringBuilder.append(String.valueOf(String.valueOf(card.getAddWater())) + ",");
          if (pc.getQuest().get_step(card.getQuestId()) != 0) {
            stringBuilder.append("[已存入],");
          } else {
            stringBuilder.append("未侦测[存入],");
          } 
          String[] clientStrAry = stringBuilder.toString().split(",");
          pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "card_0", clientStrAry));
          ok = true;
          break;
        } 
        i++;
      } 
      if (ok)
        return true; 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return false;
  }
}
